﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class showHasharsForm : Form
    {
        public showHasharsForm()
        {
            InitializeComponent();
            this.ShowInTaskbar = false;
        }

        int regID, avpID, rowInd =-1;
        tb_hashar hashar;

        private void chbDate_CheckedChanged(object sender, EventArgs e)
        {
            groupBox2.Enabled = chbDate.Checked;
        }

        private void rbFicsDate_CheckedChanged(object sender, EventArgs e)
        {
            dtpTo.Enabled = label4.Enabled = rbBetween.Checked;
        }

        private void showHasharsForm_Load(object sender, EventArgs e)
        {
            cbRegions.SelectedIndexChanged -= cbRegions_SelectedIndexChanged;
            cbRegions.DataSource = UtilClass.FillCBRegions();
            cbRegions.DisplayMember = "Name";
            cbRegions.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegions.SelectedIndex = -1;
                cbRegions.Text = string.Empty;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);
            }
            else
            {
                cbRegions.Enabled = false;
                regID = InformationClass.regionID;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);
                cbRegions.SelectedValue = regID;
                cbRegions_SelectedIndexChanged(cbRegions, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
            }
        }

        private void cbRegions_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegions.SelectedValue);
            cbAVP.Enabled = true;
            //cbAVP.Items.Clear();
            //cbAVP.Text = string.Empty;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty; 
            //cbAVP.SelectedIndex = -1;
            ShowHasharsButton.Enabled = false;
            cbAVP.SelectedIndexChanged+=new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                ShowHasharsButton.Enabled = true;
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
            }
        }

        private void ShowHasharsButton_Click(object sender, EventArgs e)
        {
            ShowHashars();
            ShowHasharInfo();
            
        }


        private void ShowHashars()
        {
            var result = DBClass.Hashars.Where(wh => wh.AVPID == avpID);
            if (chbDate.Checked)
            {
                if (rbFicsDate.Checked)
                    result = result.Where(wh => wh.Date == dtpFrom.Value.Date);
                else
                    result = result.Where(wh => wh.Date >= dtpFrom.Value.Date && wh.Date <= dtpTo.Value.Date);
            }
            dgvHashars.DataSource = result
                .Select(se => new
                {
                    hDate = se.Date.ToShortDateString(),
                    hDCount = se.DaysCount,
                    hPeopleCount = se.PeopleCount,
                    hDetails = se.Details
                })
                .ToList();

            if (InformationClass.Lang)
            {
                dgvHashars.Columns["hDate"].HeaderText = "Дата проведения";
                dgvHashars.Columns["hDCount"].HeaderText = "Количество дней";
                dgvHashars.Columns["hPeopleCount"].HeaderText = "Количество людей";
                dgvHashars.Columns["hDetails"].HeaderText = "Объект и виды работ";
            }
            else
            {
                dgvHashars.Columns["hDate"].HeaderText = "Ўтказиш санаси";
                dgvHashars.Columns["hDCount"].HeaderText = "Кунлар сони";
                dgvHashars.Columns["hPeopleCount"].HeaderText = "Кишилар сони";
                dgvHashars.Columns["hDetails"].HeaderText = "Объект ва ишлар тури";
            }


            dgvHashars.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHashars.AutoResizeColumns();
        }

        private void dgvHashars_SelectionChanged(object sender, EventArgs e)
        {
            if (rowInd != dgvHashars.CurrentRow.Index && dgvHashars.RowCount > 0)
            {
                rowInd = dgvHashars.CurrentRow.Index;
                ShowHasharInfo();
            }
        }

        private void ShowHasharInfo()
        {
            if (dgvHashars.CurrentRow == null)
                return;

            hashar = DBClass.Hashars.Where(wh => wh.AVPID == avpID && wh.Date == DateTime.Parse(dgvHashars["hDate", dgvHashars.CurrentRow.Index].Value.ToString())).SingleOrDefault();
            if (hashar != null)
            {
                dtpDate.Value = hashar.Date;
                tbDCount.Text = hashar.DaysCount.Value.ToString();
                tbPCount.Text = hashar.PeopleCount.Value.ToString();
                tbDetails.Text = hashar.Details;
            }
        }

        private void buttonEditHashar_Click(object sender, EventArgs e)
        {
            int days, peoples;
            if(!(Int32.TryParse(tbDCount.Text, out days) && Int32.TryParse(tbPCount.Text, out peoples)))
            {
                MessageBox.Show("Количесво дней и человек введенно не корректно");
                return;
            }

            if (days == 0 || peoples == 0)
            {
                MessageBox.Show("Количесво дней и человек должно быть отлично от 0");
                return;
            }

            if (hashar != null)
            {
                hashar.Date = dtpDate.Value.Date;
                hashar.DaysCount = days;
                hashar.PeopleCount = peoples;
                hashar.Details = tbDetails.Text;

                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung); 
                    ShowHashars();
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
                
            }
        }
    }
}
